<?php
	// using MySQL extension
	$link = mysql_connect('localhost', 'root', '') or
		die('Connection error:'.mysql_error());
	$db = mysql_select_db('TEST1') or
		die('Cannot select database:'.mysql_error());
	$result = mysql_query("SELECT * from artists") or
		die("Query error:".mysql_error());
	while ($row = mysql_fetch_array($result)) {
		printf("Artist ID : %d Artist Name: %s <br/>",
			$row['artist_id'], $row['artist_name']);
	}
	mysql_free_result($result);
	mysql_close($link);

	// using MySQL extension Improved
	$link = new mysqli("localhost", "root", "", "TEST1");
	if ($link->connect_error) {
		die("Connection error".$link->error());
	}
	$sql = "SELECT album_name from albums";
	$result = $link->query($sql);
	while ($row = $result->fetch_assoc()) {
		printf("Album Name: %s <br/>", $row['album_name']);
	}
	$link->close();

	// using prepared query
	$link = new mysqli("localhost", "root", "", "TEST1");
	if ($link->connect_error) {
		die("Connection error".$link->error());
	}
	$sql = "SELECT album_name from albums where album_id = ?";
	$stmt = $link->prepare($sql);
	$id = 1;
	$stmt->bind_param('i', $id);
	for (; $id != 8; ++$id) {
		$stmt->execute();
		// number of argument must match the number of field
		$stmt->bind_result($result);
		$stmt->fetch();
		echo $result;
		echo "<br/>";
	}
	$link->close();

	// using PHP Data Objects (PDO)
	try {
		$link = new PDO("mysql:host=localhost;dbname=TEST1",
				"root", "");
	} catch (PDOException $e) {
		print "error! ".$e->getMessage()."<br/>";
		die();
	}
	$stmt = $link->prepare(
			"SELECT album_name from albums where album_id >= ?");
	// pass an array to replace the ? s
	$stmt->execute(array(5));
	// fetch a associative array
	while ($row = $stmt->fetch()) {
		printf("%s <br />", $row["album_name"]);
	}

?>